{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python数值计算库NumPy\n",
    "#  —— 一切向量化计算的基础\n",
    "\n",
    "\n",
    "# 1. NumPy初探"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1 开始使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from numpy import cos,sin #避免使用\n",
    "import numpy as np #np.method()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 从三种“列表“定义开始"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "r1 = range(5)\n",
    "r2 = np.arange(5)\n",
    "r3 = xrange(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4] [0 1 2 3 4] xrange(5)\n"
     ]
    }
   ],
   "source": [
    "print r1,r2,r3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 2 3 4 \n",
      "\n",
      "0 1 2 3 4 \n",
      "\n",
      "0 1 2 3 4\n"
     ]
    }
   ],
   "source": [
    "for i in r1:\n",
    "    print i,\n",
    "print '\\n'\n",
    "for i in r2:\n",
    "    print i,\n",
    "print '\\n'\n",
    "for i in r3:\n",
    "    print i,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<type 'list'> <type 'numpy.ndarray'> <type 'xrange'>\n"
     ]
    }
   ],
   "source": [
    "print type(r1),type(r2),type(r3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4] [] [5 4 3 2 1]\n"
     ]
    }
   ],
   "source": [
    "print np.arange(0,5),np.arange(5,0),np.arange(5,0,-1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### IPython能够支持自动补全和帮助："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function numpy.core.multiarray.arange>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange #Tab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "np.arange?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 论numpy的正确打开方式：少用原生语法、多用ufunc和broadcasting，少用数据转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def vecadd(n):\n",
    "    a = range(n)\n",
    "    b = range(n)\n",
    "    c = []\n",
    "    for i in range(n):\n",
    "        a[i] = i*2\n",
    "        b[i] = i+3\n",
    "        c.append(a[i]+b[i])\n",
    "    return c\n",
    "\n",
    "def np_vecadd(n):\n",
    "    a = np.arange(n)*2\n",
    "    b = np.arange(n)+3\n",
    "    c = a+b\n",
    "    return c\n",
    "\n",
    "N = 10000000"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "为了比较性能，使用ipython的“魔法函数”timeit，或者datetime库的计时函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 loops, best of 3: 3.11 s per loop\n"
     ]
    }
   ],
   "source": [
    "%timeit vecadd(N)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 loops, best of 3: 108 ms per loop\n"
     ]
    }
   ],
   "source": [
    "%timeit np_vecadd(N)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.timedelta(0, 3, 686897)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import datetime\n",
    "t1 = datetime.datetime.now()\n",
    "result = vecadd(N)\n",
    "t2 = datetime.datetime.now()\n",
    "t2-t1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 4, 5, 6, 7])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(5)+3 #ufunc or broadcasting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 2, 4, 6, 8])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(5)*2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.   0.2  0.4  0.6  0.8  1.   1.2  1.4  1.6  1.8  2. ] \n",
      "\n",
      "[ 0.   0.2  0.4  0.6  0.8  1.   1.2  1.4  1.6  1.8  2. ]\n"
     ]
    }
   ],
   "source": [
    "a,b,c = 0,2,11\n",
    "print np.linspace(a,b,c),'\\n'*2,np.arange(c)/float(c-1)*(b-a)+a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 1.2 Numpy数据类型理解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4] <type 'numpy.ndarray'> int64 (5,)\n"
     ]
    }
   ],
   "source": [
    "#使用np.ndarray类型的r2\n",
    "print r2,type(r2),r2.dtype,r2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3 4]\n",
      " [0 1 2 3 4]] \n",
      "\n",
      "[[0 1 2 3 4]\n",
      " [0 1 2 3 4]] \n",
      "\n",
      "[array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4])]\n",
      "[[0 1 2 3 4]\n",
      " [0 1 2 3 4]] \n",
      "\n",
      "[[0 1 2 3 4]\n",
      " [0 1 2 3 4]] \n",
      "\n",
      "[array([ 0,  1, 10,  3,  4]), array([ 0,  1, 10,  3,  4])]\n"
     ]
    }
   ],
   "source": [
    "#Immunity of Mutables\n",
    "arr1 = np.array([np.arange(5),np.arange(5)])\n",
    "arr2 = np.array([r2,r2])\n",
    "arr3 = [r2,r2]\n",
    "print arr1,'\\n'*2,arr2,'\\n'*2,arr3\n",
    "r2[2] = 10\n",
    "print arr1,'\\n'*2,arr2,'\\n'*2,arr3\n",
    "r2[2] = 2\n",
    "#不需要关心类型是否可变，在np.array创建过程中就已经建立独立拷贝"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3 4]\n",
      " [0 1 2 3 4]] (2, 5)\n"
     ]
    }
   ],
   "source": [
    "print arr1,arr1.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 2 int64\n"
     ]
    }
   ],
   "source": [
    "print arr1[0,0],arr1[1,2],arr1.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.]\n",
      " [ 0.  1.  2.  3.  4.]]\n"
     ]
    }
   ],
   "source": [
    "print np.float64(arr1) #正确"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  0.00000000e+000   4.94065646e-324   9.88131292e-324   1.48219694e-323\n",
      "    1.97626258e-323]\n",
      " [  0.00000000e+000   4.94065646e-324   9.88131292e-324   1.48219694e-323\n",
      "    1.97626258e-323]]\n",
      "[[0 1 2 3 4]\n",
      " [0 1 2 3 4]]\n"
     ]
    }
   ],
   "source": [
    "arr1.dtype = np.float64 #错误\n",
    "print arr1\n",
    "arr1.dtype = np.int64\n",
    "print arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('64bit', '')"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# bool : 单个bit的布尔变量，仅存True或者False\n",
    "# inti : 当前平台的整数类型，int32或int64\n",
    "import platform\n",
    "platform.architecture()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "*   int8:     -128   ~ 127\n",
    "*   int16:    -32768 ~ 32767\n",
    "*   int32:    -$2^{31}$ ~ $2^{31}$-1\n",
    "*   int64:    -$2^{63}$ ~ $2^{63}$-1\n",
    "*   uint8:         0 ~ 255\n",
    "*   uint16:        0 ~ 65535\n",
    "*   uint32:        0 ~ $2^{32}$-1\n",
    "*   uint64:        0 ~ $2^{64}$-1\n",
    "*   float16:   符号＋5bit指数＋10bit小数\n",
    "*   float32:   符号＋8bit指数＋23bit小数\n",
    "*   float64:   符号＋11bit指数＋52bit小数\n",
    "*   complex64:  单精度浮点复数\n",
    "*   complex128: 双精度浮点复数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.0\n"
     ]
    }
   ],
   "source": [
    "i = np.float64(3)\n",
    "print i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0 2.0\n"
     ]
    }
   ],
   "source": [
    "c = np.complex128(1+2j)\n",
    "print c.real,c.imag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8 16\n"
     ]
    }
   ],
   "source": [
    "print i.dtype.itemsize,c.dtype.itemsize"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Python不显示指定数据类型与精度，如何控制数据类型？Numpy自定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('sku_id', '<i4'), ('desc', 'S50'), ('value', '<f8')]\n",
      "[('sku_id', '<i4'), ('desc', 'S50'), ('value', '<f8')]\n"
     ]
    }
   ],
   "source": [
    "sku = np.dtype([('sku_id',np.int32),('desc',np.str_,50),('value',np.float64)])\n",
    "print sku\n",
    "sku2 = np.dtype({'names':['sku_id','desc','value'],'formats':['<i4','S50','<f8']})\n",
    "print sku2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(1, 'apple', 2.3) (2, '5', 5.0) (3, 'banana', 1.0)]\n"
     ]
    }
   ],
   "source": [
    "online_shop = np.array([(1,'apple',2.3),(2.1,5,5),(3,'banana',True)],dtype=sku)\n",
    "print online_shop"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 1.3 Numpy数据操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "#### 数据切片（Slicing）变形（Reshaping）拼接（Stacking）与拆分（Splitting）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11] (12,)\n"
     ]
    }
   ],
   "source": [
    "arr = np.arange(12)\n",
    "print arr,arr.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 4] [7 8 9] [3 8]\n"
     ]
    }
   ],
   "source": [
    "print arr[3:5],arr[-5:-2],arr[3:28:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1]\n",
      "  [ 2  3]]\n",
      "\n",
      " [[ 4  5]\n",
      "  [ 6  7]]\n",
      "\n",
      " [[ 8  9]\n",
      "  [10 11]]]\n"
     ]
    }
   ],
   "source": [
    "arr = arr.reshape(3,2,2)\n",
    "print arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]] \n",
      "2 \n",
      "(3, 4)\n"
     ]
    }
   ],
   "source": [
    "arr.resize((3,4))\n",
    "print arr,'\\n',arr.ndim,'\\n',arr.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]] \n",
      "\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n"
     ]
    }
   ],
   "source": [
    "print arr.T,'\\n'*2,arr.transpose()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "arrflat = arr.flat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<numpy.flatiter object at 0x1021d2600>\n"
     ]
    }
   ],
   "source": [
    "print arrflat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11]\n"
     ]
    }
   ],
   "source": [
    "print np.array([item for item in arrflat],dtype=np.int64)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11] \n",
      "\n",
      "[ 0  4  8  1  5  9  2  6 10  3  7 11] \n",
      "\n",
      "<type 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "print arr.flatten(order='C'),'\\n'*2, arr.flatten(order='F'),'\\n'*2,type(arr.flatten())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11] \n",
      "\n",
      "<type 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "print arr.ravel(),'\\n'*2,type(arr.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]] \n",
      "\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]] \n",
      "\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "print np.vstack((arr,arr)),'\\n'*2,np.row_stack((arr,arr)),'\\n'*2, np.concatenate((arr,arr),axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  0  1  2  3]\n",
      " [ 4  5  6  7  4  5  6  7]\n",
      " [ 8  9 10 11  8  9 10 11]] \n",
      "\n",
      "[[ 0  1  2  3  0  1  2  3]\n",
      " [ 4  5  6  7  4  5  6  7]\n",
      " [ 8  9 10 11  8  9 10 11]] \n",
      "\n",
      "[[ 0  1  2  3  0  1  2  3]\n",
      " [ 4  5  6  7  4  5  6  7]\n",
      " [ 8  9 10 11  8  9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "print np.hstack((arr,arr)),'\\n'*2,np.column_stack((arr,arr)),'\\n'*2, np.concatenate((arr,arr),axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  0]\n",
      "  [ 1  1]\n",
      "  [ 2  2]\n",
      "  [ 3  3]]\n",
      "\n",
      " [[ 4  4]\n",
      "  [ 5  5]\n",
      "  [ 6  6]\n",
      "  [ 7  7]]\n",
      "\n",
      " [[ 8  8]\n",
      "  [ 9  9]\n",
      "  [10 10]\n",
      "  [11 11]]]\n"
     ]
    }
   ],
   "source": [
    "print np.dstack((arr,arr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "arr.resize((3,4,1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0]\n",
      "  [ 1]\n",
      "  [ 2]\n",
      "  [ 3]]\n",
      "\n",
      " [[ 4]\n",
      "  [ 5]\n",
      "  [ 6]\n",
      "  [ 7]]\n",
      "\n",
      " [[ 8]\n",
      "  [ 9]\n",
      "  [10]\n",
      "  [11]]] \n",
      "\n",
      "[[[ 0  0]\n",
      "  [ 1  1]\n",
      "  [ 2  2]\n",
      "  [ 3  3]]\n",
      "\n",
      " [[ 4  4]\n",
      "  [ 5  5]\n",
      "  [ 6  6]\n",
      "  [ 7  7]]\n",
      "\n",
      " [[ 8  8]\n",
      "  [ 9  9]\n",
      "  [10 10]\n",
      "  [11 11]]]\n"
     ]
    }
   ],
   "source": [
    "print arr,'\\n'*2,np.concatenate((arr,arr),axis=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0]\n",
      "  [ 1]\n",
      "  [ 2]\n",
      "  [ 3]]\n",
      "\n",
      " [[ 4]\n",
      "  [ 5]\n",
      "  [ 6]\n",
      "  [ 7]]\n",
      "\n",
      " [[ 8]\n",
      "  [ 9]\n",
      "  [10]\n",
      "  [11]]] \n",
      "\n",
      "[[[ 0  0  0]\n",
      "  [ 1  1  1]\n",
      "  [ 2  2  2]\n",
      "  [ 3  3  3]]\n",
      "\n",
      " [[ 4  4  4]\n",
      "  [ 5  5  5]\n",
      "  [ 6  6  6]\n",
      "  [ 7  7  7]]\n",
      "\n",
      " [[ 8  8  8]\n",
      "  [ 9  9  9]\n",
      "  [10 10 10]\n",
      "  [11 11 11]]]\n"
     ]
    }
   ],
   "source": [
    "print arr,'\\n'*2,np.tile(arr,(1,1,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[0 1]\n",
      "  [2 3]]\n",
      "\n",
      " [[4 5]\n",
      "  [6 7]]]\n"
     ]
    }
   ],
   "source": [
    "arr2 = np.arange(8).reshape(2,2,2)\n",
    "print arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([[[0, 1],\n",
      "        [2, 3]]]), array([[[4, 5],\n",
      "        [6, 7]]])] \n",
      "\n",
      "[array([[[0, 1],\n",
      "        [2, 3]]]), array([[[4, 5],\n",
      "        [6, 7]]])]\n"
     ]
    }
   ],
   "source": [
    "print np.vsplit(arr2,2), '\\n'*2, np.split(arr2,2,axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([[[0, 1]],\n",
      "\n",
      "       [[4, 5]]]), array([[[2, 3]],\n",
      "\n",
      "       [[6, 7]]])] \n",
      "\n",
      "[array([[[0, 1]],\n",
      "\n",
      "       [[4, 5]]]), array([[[2, 3]],\n",
      "\n",
      "       [[6, 7]]])]\n"
     ]
    }
   ],
   "source": [
    "print np.hsplit(arr2,2), '\\n'*2, np.split(arr2,2,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([[[0],\n",
      "        [2]],\n",
      "\n",
      "       [[4],\n",
      "        [6]]]), array([[[1],\n",
      "        [3]],\n",
      "\n",
      "       [[5],\n",
      "        [7]]])] \n",
      "\n",
      "[array([[[0],\n",
      "        [2]],\n",
      "\n",
      "       [[4],\n",
      "        [6]]]), array([[[1],\n",
      "        [3]],\n",
      "\n",
      "       [[5],\n",
      "        [7]]])]\n"
     ]
    }
   ],
   "source": [
    "print np.dsplit(arr2,2), '\\n'*2, np.split(arr2,2,axis=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2 NumPy数学函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.0 使用Random\n",
    "\n",
    "使用其中的rand、randint等方法生成随机数来方便后续的测试。\n",
    "\n",
    "随机数生成函数与统计分布将在统计基础课程中另行介绍。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 3 3 1 5 4 2 3 5 2 3 4 5 4 1]\n"
     ]
    }
   ],
   "source": [
    "arr_random = np.random.randint(1,6,15)\n",
    "print arr_random"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### 2.1 数学函数的测试\n",
    "\n",
    "统计函数返回一个值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5 1 3.13333333333 1.71555555556 1.30979218029\n"
     ]
    }
   ],
   "source": [
    "print arr_random.max(),arr_random.min(),arr_random.mean(),arr_random.var(),arr_random.std()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ufunc类函数返回逐个计算值：\n",
    "\n",
    "很多Ufunc即Universal Functional在Python中是以C语言级别实现的，效率高，建议使用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.69314718  1.09861229  1.09861229  0.          1.60943791  1.38629436\n",
      "  0.69314718  1.09861229  1.60943791  0.69314718  1.09861229  1.38629436\n",
      "  1.60943791  1.38629436  0.        ] \n",
      "\n",
      "[   7.3890561    20.08553692   20.08553692    2.71828183  148.4131591\n",
      "   54.59815003    7.3890561    20.08553692  148.4131591     7.3890561\n",
      "   20.08553692   54.59815003  148.4131591    54.59815003    2.71828183] \n",
      "\n",
      "[False  True  True False  True  True False  True  True False  True  True\n",
      "  True  True False] \n",
      "\n",
      "[0 1 1 1 1 0 0 1 1 0 1 0 1 0 1]\n"
     ]
    }
   ],
   "source": [
    "print np.log(arr_random),'\\n'*2,np.exp(arr_random),'\\n'*2,arr_random>2,'\\n'*2,np.mod(arr_random,2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "过滤值：类似SAS的Compress（其实是反过来）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 3, 4, 5, 4, 4, 3])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr_random.compress(arr_random>2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "过滤值：类似R语法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 3, 5, 4, 3, 5, 3, 4, 5, 4])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr_random[arr_random>2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "类似R的语法中，用于过滤条件的np.array是可以进行布尔操作的："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 3 3 2 3 2 3] [1 5 4 5 4 5 4 1]\n"
     ]
    }
   ],
   "source": [
    "print arr_random[(arr_random>1) & (arr_random<4)],\n",
    "print arr_random[(arr_random>1) ^ (arr_random<4)]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python的Clip，对值进行裁剪"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3, 3, 2, 4, 4, 2, 3, 4, 2, 3, 4, 4, 4, 2])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr_random.clip(2,4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以方便的进行时间序列操作——差分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.50319447  0.85476985  0.69350054  0.85602088  0.39440826  0.82899253\n",
      "  0.02564264  0.18737798  0.34984262  0.94389631] \n",
      "\n",
      "[ 0.35157538 -0.16126931  0.16252034 -0.46161263  0.43458427 -0.80334988\n",
      "  0.16173534  0.16246464  0.59405369]\n"
     ]
    }
   ],
   "source": [
    "arr_random = np.random.rand(10)\n",
    "print arr_random,'\\n'*2,np.diff(arr_random) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "差分其实是一种最简单的卷积："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.50319447  0.85476985  0.69350054  0.85602088  0.39440826  0.82899253\n",
      "  0.02564264  0.18737798  0.34984262  0.94389631] \n",
      "\n",
      "[ 0.50319447  0.35157538 -0.16126931  0.16252034 -0.46161263  0.43458427\n",
      " -0.80334988  0.16173534  0.16246464  0.59405369 -0.94389631] \n",
      "\n",
      "[ 0.35157538 -0.16126931  0.16252034 -0.46161263  0.43458427 -0.80334988\n",
      "  0.16173534  0.16246464  0.59405369]\n"
     ]
    }
   ],
   "source": [
    "w = np.array([1,-1]) \n",
    "len = 2\n",
    "print arr_random,'\\n'*2,np.convolve(w,arr_random),'\\n'*2,np.convolve(w,arr_random)[len-1:-len+1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "差分的反向操作——累计加和（累积乘法类似）："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.50319447  0.35157538 -0.16126931  0.16252034 -0.46161263  0.43458427\n",
      " -0.80334988  0.16173534  0.16246464  0.59405369]\n",
      "[ 0.50319447  0.85476985  0.69350054  0.85602088  0.39440826  0.82899253\n",
      "  0.02564264  0.18737798  0.34984262  0.94389631] \n",
      "\n",
      "[  5.03194468e-01   1.76910787e-01  -2.85302802e-02  -4.63675088e-03\n",
      "   2.14038276e-03   9.30176676e-04  -7.47257324e-04  -1.20857914e-04\n",
      "  -1.96351376e-05  -1.16643260e-05]\n"
     ]
    }
   ],
   "source": [
    "diff = np.convolve(w,arr_random)[0:-1]\n",
    "print diff\n",
    "print diff.cumsum(),'\\n'*2,diff.cumprod()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "排序时间到了：给出排序结果，本地排序，并保留原数组下标位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "arr_random = np.random.rand(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.08786745  0.09802729  0.15244481  0.15467132  0.18330197  0.2293121\n",
      "  0.29645834  0.52844745  0.72392558  0.87188892]\n"
     ]
    }
   ],
   "source": [
    "print np.sort(arr_random)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.15244481  0.15467132  0.29645834  0.87188892  0.09802729  0.08786745\n",
      "  0.72392558  0.52844745  0.2293121   0.18330197]\n"
     ]
    }
   ],
   "source": [
    "print arr_random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.08786745  0.09802729  0.15244481  0.15467132  0.18330197  0.2293121\n",
      "  0.29645834  0.52844745  0.72392558  0.87188892]\n"
     ]
    }
   ],
   "source": [
    "arr_random.sort()\n",
    "print arr_random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "arr_random = np.random.rand(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用enumerate和sorted排序：\n",
    "\n",
    "关于更多匿名函数（Lambda）使用，请参考Python函数式编程，也就是Functional Programming相关内容。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(4, 0.12863450206826932), (9, 0.32980836827086768), (2, 0.33491594197762742), (1, 0.33654387285035214), (3, 0.40929439162767556), (7, 0.42846880907613405), (6, 0.50874654192233604), (5, 0.58841594297657973), (8, 0.67497111346825522), (0, 0.99078322046068001)]\n"
     ]
    }
   ],
   "source": [
    "s = sorted(enumerate(arr_random),key=lambda x:x[1],reverse=False)\n",
    "print s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 9, 2, 1, 3, 7, 6, 5, 8, 0)"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "zip(*s)[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<type 'enumerate'>\n",
      "0 0.990783220461\n",
      "1 0.33654387285\n",
      "2 0.334915941978\n",
      "3 0.409294391628\n",
      "4 0.128634502068\n",
      "5 0.588415942977\n",
      "6 0.508746541922\n",
      "7 0.428468809076\n",
      "8 0.674971113468\n",
      "9 0.329808368271\n",
      "[(0, 0.99078322046068001), (1, 0.33654387285035214), (2, 0.33491594197762742), (3, 0.40929439162767556), (4, 0.12863450206826932), (5, 0.58841594297657973), (6, 0.50874654192233604), (7, 0.42846880907613405), (8, 0.67497111346825522), (9, 0.32980836827086768)]\n"
     ]
    }
   ],
   "source": [
    "print type(enumerate(arr_random))\n",
    "for idx,value in enumerate(arr_random):\n",
    "    print idx,value\n",
    "print zip(xrange(10),arr_random)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4 9 2 1 3 7 6 5 8 0]\n"
     ]
    }
   ],
   "source": [
    "print np.lexsort((np.arange(10),arr_random)) #接受一个参数，传入元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4 9 2 1 3 7 6 5 8 0]\n"
     ]
    }
   ],
   "source": [
    "print np.argsort(arr_random) #相当于lexsort参数元组第一列给下标"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3 二维数组与矩阵操作\n",
    "\n",
    "### 3.1 基本定义\n",
    "\n",
    "直接定义值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "arr2d_random = np.random.rand(10).reshape(5,2)\n",
    "arr2d_zeros = np.zeros_like(arr2d_random)\n",
    "arr2d_ones = np.ones_like(arr2d_random)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.67758622  0.37036523]\n",
      " [ 0.10966036  0.81607858]\n",
      " [ 0.82083969  0.11857728]\n",
      " [ 0.6070126   0.83232989]\n",
      " [ 0.27118225  0.09858034]] \n",
      "\n",
      "[[ 0.  0.]\n",
      " [ 0.  0.]\n",
      " [ 0.  0.]\n",
      " [ 0.  0.]\n",
      " [ 0.  0.]] \n",
      "\n",
      "[[ 1.  1.]\n",
      " [ 1.  1.]\n",
      " [ 1.  1.]\n",
      " [ 1.  1.]\n",
      " [ 1.  1.]]\n"
     ]
    }
   ],
   "source": [
    "print arr2d_random,'\\n'*2,arr2d_zeros,'\\n'*2,arr2d_ones"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "直接填充常用值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  0.]\n",
      " [ 0.  0.]\n",
      " [ 0.  0.]\n",
      " [ 0.  0.]\n",
      " [ 0.  0.]] \n",
      "\n",
      "[[ 1.  1.]\n",
      " [ 1.  1.]\n",
      " [ 1.  1.]\n",
      " [ 1.  1.]\n",
      " [ 1.  1.]] \n",
      "\n",
      "[[ 1.  0.  0.]\n",
      " [ 0.  1.  0.]\n",
      " [ 0.  0.  1.]]\n"
     ]
    }
   ],
   "source": [
    "print np.zeros((5,2)), '\\n'*2 , np.ones((5,2)),'\\n'*2,np.eye(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "善于利用函数者，可以使用函数，传入下标作为参数，生成二维数组：\n",
    "\n",
    "”首相卡梅隆“函数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  1.   2.   3.   4.   5.   6.   7.   8.   9.]\n",
      " [  2.   4.   6.   8.  10.  12.  14.  16.  18.]\n",
      " [  3.   6.   9.  12.  15.  18.  21.  24.  27.]\n",
      " [  4.   8.  12.  16.  20.  24.  28.  32.  36.]\n",
      " [  5.  10.  15.  20.  25.  30.  35.  40.  45.]\n",
      " [  6.  12.  18.  24.  30.  36.  42.  48.  54.]\n",
      " [  7.  14.  21.  28.  35.  42.  49.  56.  63.]\n",
      " [  8.  16.  24.  32.  40.  48.  56.  64.  72.]\n",
      " [  9.  18.  27.  36.  45.  54.  63.  72.  81.]]\n"
     ]
    }
   ],
   "source": [
    "def CameronThePrimeMinister(i,j):\n",
    "    return (i+1)*(j+1)\n",
    "\n",
    "print np.fromfunction(CameronThePrimeMinister,(9,9))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "习惯R者，使用broadcasting，生成二维数组，更具体的原理和应用在后续介绍：\n",
    "\n",
    "9 \\* 1的二维数组和长度为9的一维数组broadcasting两次，互相补齐对方较短的维度："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1]\n",
      " [2]\n",
      " [3]\n",
      " [4]\n",
      " [5]\n",
      " [6]\n",
      " [7]\n",
      " [8]\n",
      " [9]] \n",
      "\n",
      "[1 2 3 4 5 6 7 8 9] \n",
      "\n",
      "[[ 1  2  3  4  5  6  7  8  9]\n",
      " [ 2  4  6  8 10 12 14 16 18]\n",
      " [ 3  6  9 12 15 18 21 24 27]\n",
      " [ 4  8 12 16 20 24 28 32 36]\n",
      " [ 5 10 15 20 25 30 35 40 45]\n",
      " [ 6 12 18 24 30 36 42 48 54]\n",
      " [ 7 14 21 28 35 42 49 56 63]\n",
      " [ 8 16 24 32 40 48 56 64 72]\n",
      " [ 9 18 27 36 45 54 63 72 81]]\n"
     ]
    }
   ],
   "source": [
    "x = np.arange(1,10)\n",
    "\n",
    "print x.reshape(-1,1),'\\n'*2,x,'\\n'*2,x.reshape(-1,1) * x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "更进一步缩减代码，生成正交栅格用于广播的方法，支持灵活的运算：\n",
    "\n",
    "状如切片，功能如range/linspace，实数第三参数代表步长，虚数第三参数代表总长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0. ]\n",
      " [ 0.2]\n",
      " [ 0.4]\n",
      " [ 0.6]\n",
      " [ 0.8]\n",
      " [ 1. ]] [[ 0.   0.2  0.4  0.6  0.8  1. ]]\n",
      "[[ 1.          0.96078944  0.85214379  0.69767633  0.52729242  0.36787944]\n",
      " [ 0.96078944  0.92311635  0.81873075  0.67032005  0.50661699  0.35345468]\n",
      " [ 0.85214379  0.81873075  0.72614904  0.59452055  0.44932896  0.31348618]\n",
      " [ 0.69767633  0.67032005  0.59452055  0.48675226  0.36787944  0.25666078]\n",
      " [ 0.52729242  0.50661699  0.44932896  0.36787944  0.2780373   0.19398004]\n",
      " [ 0.36787944  0.35345468  0.31348618  0.25666078  0.19398004  0.13533528]]\n"
     ]
    }
   ],
   "source": [
    "x,y = np.ogrid[0:1:6j,0:1:6j]\n",
    "print x,y\n",
    "print np.exp(-x**2-y**2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "习惯数据库的笛卡尔积者，可以用outer关键字进行某些运算，支持传入iterable更能节省空间和时间："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9],\n",
       "       [ 2,  4,  6,  8, 10, 12, 14, 16, 18],\n",
       "       [ 3,  6,  9, 12, 15, 18, 21, 24, 27],\n",
       "       [ 4,  8, 12, 16, 20, 24, 28, 32, 36],\n",
       "       [ 5, 10, 15, 20, 25, 30, 35, 40, 45],\n",
       "       [ 6, 12, 18, 24, 30, 36, 42, 48, 54],\n",
       "       [ 7, 14, 21, 28, 35, 42, 49, 56, 63],\n",
       "       [ 8, 16, 24, 32, 40, 48, 56, 64, 72],\n",
       "       [ 9, 18, 27, 36, 45, 54, 63, 72, 81]])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.multiply.outer(xrange(1,10),xrange(1,10))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "二维数组和矩阵有一些不同：\n",
    "\n",
    "矩阵可以方便的求逆、直接做乘法，二维数组的＊只能作为np.multiply而不是np.dot："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.08765291 -0.035521  ]\n",
      " [-0.035521    0.12995714]]\n"
     ]
    }
   ],
   "source": [
    "cov =  np.cov(arr2d_random.T)\n",
    "print cov"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.29606233  0.        ]\n",
      " [ 0.          0.36049569]]\n"
     ]
    }
   ],
   "source": [
    "stdiag = np.diag(np.sqrt(np.diag(cov)))\n",
    "print stdiag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 3.37766708  0.        ]\n",
      " [ 0.          2.77395825]]\n"
     ]
    }
   ],
   "source": [
    "invstdiag = np.array(np.mat(stdiag).I)\n",
    "print invstdiag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1., -0.],\n",
       "       [-0.,  1.]])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#错误\n",
    "invstdiag*cov*invstdiag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 1.       , -0.3328143],\n",
       "        [-0.3328143,  1.       ]])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#正确\n",
    "np.mat(stdiag).I*np.mat(cov)*np.mat(stdiag).I"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.       , -0.3328143],\n",
       "       [-0.3328143,  1.       ]])"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#验证\n",
    "np.corrcoef(arr2d_random.T)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###一般最小二乘法求解线性回归（OLS）\n",
    "\n",
    "要进行OLS，核心是矩阵求逆：\n",
    "\n",
    "$ y = X \\beta + \\beta_0 \\rightarrow y ＝ X \\beta + \\beta_0 + \\epsilon \\rightarrow \\hat{\\beta} = (X^TX)^{-1}X^Ty $\n",
    "\n",
    "随机制造数据来做个OLS的例子："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = np.random.rand(30).reshape(15,2)\n",
    "y = x[:,0]*0.7 - x[:,1]*0.2 + 0.1+0.1*np.random.rand(15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.55541422  0.21316423  0.28526456  0.75681944  0.06493666  0.30003525\n",
      "  0.26751402  0.70417776  0.19398665  0.51653352  0.22651041  0.30310932\n",
      "  0.55547587  0.67303199  0.27000489] \n",
      "\n",
      "[[ 0.86168706  0.9843286 ]\n",
      " [ 0.29516578  0.61655325]\n",
      " [ 0.23422653  0.33417053]\n",
      " [ 0.96572639  0.52828207]\n",
      " [ 0.13055911  0.83228165]\n",
      " [ 0.55808258  0.9865468 ]\n",
      " [ 0.15394166  0.0973267 ]\n",
      " [ 0.76897767  0.16983656]\n",
      " [ 0.25024672  0.44084084]\n",
      " [ 0.65832659  0.61386939]\n",
      " [ 0.13896686  0.12434786]\n",
      " [ 0.28149567  0.22562727]\n",
      " [ 0.72422763  0.73299455]\n",
      " [ 0.91282181  0.50645161]\n",
      " [ 0.18151339  0.15076421]]\n"
     ]
    }
   ],
   "source": [
    "print y,'\\n'*2,x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.          0.86168706  0.9843286 ]\n",
      " [ 1.          0.29516578  0.61655325]\n",
      " [ 1.          0.23422653  0.33417053]\n",
      " [ 1.          0.96572639  0.52828207]\n",
      " [ 1.          0.13055911  0.83228165]\n",
      " [ 1.          0.55808258  0.9865468 ]\n",
      " [ 1.          0.15394166  0.0973267 ]\n",
      " [ 1.          0.76897767  0.16983656]\n",
      " [ 1.          0.25024672  0.44084084]\n",
      " [ 1.          0.65832659  0.61386939]\n",
      " [ 1.          0.13896686  0.12434786]\n",
      " [ 1.          0.28149567  0.22562727]\n",
      " [ 1.          0.72422763  0.73299455]\n",
      " [ 1.          0.91282181  0.50645161]\n",
      " [ 1.          0.18151339  0.15076421]]\n"
     ]
    }
   ],
   "source": [
    "Xcvr = np.mat(np.hstack((np.ones(15).reshape(15,1),x)))\n",
    "print Xcvr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "给出模型参数的估计：$\\hat{\\beta} = (X^TX)^{-1}X^Ty $"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.16623078  0.74514118 -0.2600523 ]] \n",
      "\n",
      "[[ 0.55233238  0.22583487  0.25386079  0.74845231  0.04707899  0.32552733\n",
      "   0.25562902  0.69506132  0.23805824  0.49713888  0.23744376  0.3173099\n",
      "   0.51526569  0.71470799  0.2622773 ]]\n"
     ]
    }
   ],
   "source": [
    "H = Xcvr*(Xcvr.T*Xcvr).I*Xcvr.T\n",
    "betahats = np.dot((Xcvr.T*Xcvr).I*Xcvr.T,y)\n",
    "preds = np.dot(H,y)\n",
    "print betahats,'\\n'*2,preds"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对照statsmodels的OLS结果："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/wangweiyang/anaconda/anaconda/lib/python2.7/site-packages/scipy/stats/stats.py:1233: UserWarning: kurtosistest only valid for n>=20 ... continuing anyway, n=15\n",
      "  int(n))\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table class=\"simpletable\">\n",
       "<caption>OLS Regression Results</caption>\n",
       "<tr>\n",
       "  <th>Dep. Variable:</th>            <td>y</td>        <th>  R-squared:         </th> <td>   0.987</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Model:</th>                   <td>OLS</td>       <th>  Adj. R-squared:    </th> <td>   0.985</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Method:</th>             <td>Least Squares</td>  <th>  F-statistic:       </th> <td>   448.7</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Date:</th>             <td>Sat, 06 Jun 2015</td> <th>  Prob (F-statistic):</th> <td>5.28e-12</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Time:</th>                 <td>15:42:28</td>     <th>  Log-Likelihood:    </th> <td>  34.808</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>No. Observations:</th>      <td>    15</td>      <th>  AIC:               </th> <td>  -63.62</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Df Residuals:</th>          <td>    12</td>      <th>  BIC:               </th> <td>  -61.49</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Df Model:</th>              <td>     2</td>      <th>                     </th>     <td> </td>   \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Covariance Type:</th>      <td>nonrobust</td>    <th>                     </th>     <td> </td>   \n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "    <td></td>       <th>coef</th>     <th>std err</th>      <th>t</th>      <th>P>|t|</th> <th>[95.0% Conf. Int.]</th> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>const</th> <td>    0.1662</td> <td>    0.015</td> <td>   11.182</td> <td> 0.000</td> <td>    0.134     0.199</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>x1</th>    <td>    0.7451</td> <td>    0.025</td> <td>   29.856</td> <td> 0.000</td> <td>    0.691     0.800</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>x2</th>    <td>   -0.2601</td> <td>    0.026</td> <td>  -10.159</td> <td> 0.000</td> <td>   -0.316    -0.204</td>\n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "  <th>Omnibus:</th>       <td> 0.516</td> <th>  Durbin-Watson:     </th> <td>   3.068</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Prob(Omnibus):</th> <td> 0.773</td> <th>  Jarque-Bera (JB):  </th> <td>   0.567</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Skew:</th>          <td>-0.339</td> <th>  Prob(JB):          </th> <td>   0.753</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Kurtosis:</th>      <td> 2.332</td> <th>  Cond. No.          </th> <td>    5.39</td>\n",
       "</tr>\n",
       "</table>"
      ],
      "text/plain": [
       "<class 'statsmodels.iolib.summary.Summary'>\n",
       "\"\"\"\n",
       "                            OLS Regression Results                            \n",
       "==============================================================================\n",
       "Dep. Variable:                      y   R-squared:                       0.987\n",
       "Model:                            OLS   Adj. R-squared:                  0.985\n",
       "Method:                 Least Squares   F-statistic:                     448.7\n",
       "Date:                Sat, 06 Jun 2015   Prob (F-statistic):           5.28e-12\n",
       "Time:                        15:42:28   Log-Likelihood:                 34.808\n",
       "No. Observations:                  15   AIC:                            -63.62\n",
       "Df Residuals:                      12   BIC:                            -61.49\n",
       "Df Model:                           2                                         \n",
       "Covariance Type:            nonrobust                                         \n",
       "==============================================================================\n",
       "                 coef    std err          t      P>|t|      [95.0% Conf. Int.]\n",
       "------------------------------------------------------------------------------\n",
       "const          0.1662      0.015     11.182      0.000         0.134     0.199\n",
       "x1             0.7451      0.025     29.856      0.000         0.691     0.800\n",
       "x2            -0.2601      0.026    -10.159      0.000        -0.316    -0.204\n",
       "==============================================================================\n",
       "Omnibus:                        0.516   Durbin-Watson:                   3.068\n",
       "Prob(Omnibus):                  0.773   Jarque-Bera (JB):                0.567\n",
       "Skew:                          -0.339   Prob(JB):                        0.753\n",
       "Kurtosis:                       2.332   Cond. No.                         5.39\n",
       "==============================================================================\n",
       "\n",
       "Warnings:\n",
       "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
       "\"\"\""
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import statsmodels.formula.api as sm\n",
    "model = sm.OLS(y,Xcvr).fit()\n",
    "model.summary()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Page-Rank算法的核心: 矩阵相乘\n",
    "\n",
    "关系矩阵$R(i,j)$元素表示从第i个页面到第j个页面发生了跳转。\n",
    "\n",
    "初始向量w反复乘以按行标准化后的概率转移矩阵（要保证行和全部为1）。\n",
    "\n",
    "当内存空间足够时，直接用矩阵迭代乘法将矩阵与自己相乘，空间换时间。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "rand_relation = (np.random.rand(100)>0.7).reshape(10,10)\n",
    "rand_relation.dtype=np.int8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [1, 1, 1, 0, 0, 0, 0, 1, 1, 0],\n",
       "       [0, 0, 0, 0, 1, 1, 0, 1, 1, 1],\n",
       "       [1, 1, 0, 1, 0, 0, 0, 0, 1, 0],\n",
       "       [0, 1, 0, 1, 0, 1, 1, 1, 1, 1],\n",
       "       [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],\n",
       "       [0, 1, 0, 0, 0, 0, 0, 1, 0, 0],\n",
       "       [1, 1, 1, 1, 0, 0, 0, 0, 0, 1],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 1, 0, 0],\n",
       "       [1, 0, 0, 0, 1, 0, 0, 0, 0, 0]], dtype=int8)"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_relation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "rand_relation = rand_relation-np.diag(np.diag(rand_relation))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 0, 1, 1, 0, 1, 0, 1, 0],\n",
       "       [1, 1, 1, 0, 0, 1, 0, 1, 0, 1],\n",
       "       [1, 0, 1, 1, 1, 1, 0, 0, 0, 1],\n",
       "       [1, 1, 0, 0, 1, 1, 0, 0, 0, 1],\n",
       "       [0, 0, 0, 0, 0, 0, 0, 0, 1, 0],\n",
       "       [1, 1, 1, 0, 0, 0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 1, 0, 1, 0, 1, 0, 0],\n",
       "       [1, 1, 0, 0, 1, 0, 0, 0, 1, 0],\n",
       "       [1, 0, 0, 1, 0, 0, 0, 0, 0, 1],\n",
       "       [0, 0, 0, 0, 1, 0, 0, 0, 0, 0]], dtype=int8)"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rand_relation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0.2         0.16666667  0.16666667  0.2         1.          0.33333333\n",
      "  0.33333333  0.25        0.33333333  1.        ]\n"
     ]
    }
   ],
   "source": [
    "invrowsum = 1/(np.sum(rand_relation,axis=1)+1e-12)\n",
    "print invrowsum\n",
    "diag_invrowsum = np.diag(invrowsum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.2         0.          0.          0.2         0.2         0.          0.2\n",
      "   0.          0.2         0.        ]\n",
      " [ 0.16666667  0.16666667  0.16666667  0.          0.          0.16666667\n",
      "   0.          0.16666667  0.          0.16666667]\n",
      " [ 0.16666667  0.          0.16666667  0.16666667  0.16666667  0.16666667\n",
      "   0.          0.          0.          0.16666667]\n",
      " [ 0.2         0.2         0.          0.          0.2         0.2         0.\n",
      "   0.          0.          0.2       ]\n",
      " [ 0.          0.          0.          0.          0.          0.          0.\n",
      "   0.          1.          0.        ]\n",
      " [ 0.33333333  0.33333333  0.33333333  0.          0.          0.          0.\n",
      "   0.          0.          0.        ]\n",
      " [ 0.          0.          0.          0.33333333  0.          0.33333333\n",
      "   0.          0.33333333  0.          0.        ]\n",
      " [ 0.25        0.25        0.          0.          0.25        0.          0.\n",
      "   0.          0.25        0.        ]\n",
      " [ 0.33333333  0.          0.          0.33333333  0.          0.          0.\n",
      "   0.          0.          0.33333333]\n",
      " [ 0.          0.          0.          0.          1.          0.          0.\n",
      "   0.          0.          0.        ]]\n"
     ]
    }
   ],
   "source": [
    "tpm = np.dot(diag_invrowsum,rand_relation+1e-12/10)\n",
    "tpm = (tpm>1e-12/10)*tpm\n",
    "print tpm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import scipy.sparse as ssp\n",
    "sp_tpm = ssp.csr_matrix(tpm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<10x10 sparse matrix of type '<type 'numpy.float64'>'\n",
       "\twith 37 stored elements in Compressed Sparse Row format>"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sp_tpm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<10x10 sparse matrix of type '<type 'numpy.float64'>'\n",
      "\twith 90 stored elements in Compressed Sparse Row format>\n",
      "<10x10 sparse matrix of type '<type 'numpy.float64'>'\n",
      "\twith 100 stored elements in Compressed Sparse Row format>\n",
      "<10x10 sparse matrix of type '<type 'numpy.float64'>'\n",
      "\twith 100 stored elements in Compressed Sparse Row format>\n",
      "<10x10 sparse matrix of type '<type 'numpy.float64'>'\n",
      "\twith 100 stored elements in Compressed Sparse Row format>\n",
      "<10x10 sparse matrix of type '<type 'numpy.float64'>'\n",
      "\twith 100 stored elements in Compressed Sparse Row format>\n",
      "<10x10 sparse matrix of type '<type 'numpy.float64'>'\n",
      "\twith 100 stored elements in Compressed Sparse Row format>\n",
      "<10x10 sparse matrix of type '<type 'numpy.float64'>'\n",
      "\twith 100 stored elements in Compressed Sparse Row format>\n",
      "<10x10 sparse matrix of type '<type 'numpy.float64'>'\n",
      "\twith 100 stored elements in Compressed Sparse Row format>\n",
      "<10x10 sparse matrix of type '<type 'numpy.float64'>'\n",
      "\twith 100 stored elements in Compressed Sparse Row format>\n",
      "<10x10 sparse matrix of type '<type 'numpy.float64'>'\n",
      "\twith 100 stored elements in Compressed Sparse Row format>\n",
      "[[ 0.15452327  0.11008338  0.09542497  0.08082163  0.09042249  0.04745482\n",
      "   0.02836983  0.20294049  0.10191621  0.08804292]\n",
      " [ 0.15452327  0.11008338  0.09542497  0.08082163  0.09042249  0.04745482\n",
      "   0.02836983  0.20294049  0.10191621  0.08804292]\n",
      " [ 0.15452327  0.11008338  0.09542497  0.08082163  0.09042249  0.04745482\n",
      "   0.02836983  0.20294049  0.10191621  0.08804292]\n",
      " [ 0.15452327  0.11008338  0.09542497  0.08082163  0.09042249  0.04745482\n",
      "   0.02836983  0.20294049  0.10191621  0.08804292]\n",
      " [ 0.15452327  0.11008338  0.09542497  0.08082163  0.09042249  0.04745482\n",
      "   0.02836983  0.20294049  0.10191621  0.08804292]\n",
      " [ 0.15452327  0.11008338  0.09542497  0.08082163  0.09042249  0.04745482\n",
      "   0.02836983  0.20294049  0.10191621  0.08804292]\n",
      " [ 0.15452327  0.11008338  0.09542497  0.08082163  0.09042249  0.04745482\n",
      "   0.02836983  0.20294049  0.10191621  0.08804292]\n",
      " [ 0.15452327  0.11008338  0.09542497  0.08082163  0.09042249  0.04745482\n",
      "   0.02836983  0.20294049  0.10191621  0.08804292]\n",
      " [ 0.15452327  0.11008338  0.09542497  0.08082163  0.09042249  0.04745482\n",
      "   0.02836983  0.20294049  0.10191621  0.08804292]\n",
      " [ 0.15452327  0.11008338  0.09542497  0.08082163  0.09042249  0.04745482\n",
      "   0.02836983  0.20294049  0.10191621  0.08804292]] \n",
      "\n",
      "[ 0.15452327  0.11008338  0.09542497  0.08082163  0.09042249  0.04745482\n",
      "  0.02836983  0.20294049  0.10191621  0.08804292]\n"
     ]
    }
   ],
   "source": [
    "for i in xrange(10):\n",
    "    sp_tpm = sp_tpm.dot(sp_tpm)\n",
    "    print repr(sp_tpm)\n",
    "\n",
    "sp_tpm_a = sp_tpm.toarray()\n",
    "print sp_tpm_a,'\\n'*2,sp_tpm_a[0,:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 4. 还有一件事……文件读写\n",
    "\n",
    "Numpy的分析功能虽然大部分被Pandas替代，但文件读写仍然是一个科学计算库的基本功。\n",
    "\n",
    "熟悉Matlab和R语言的人对下列语法不会陌生："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.93525653  0.76660826]\n",
      " [ 0.01380756  0.55349255]\n",
      " [ 0.59566385  0.76267376]\n",
      " [ 0.91820398  0.32110088]\n",
      " [ 0.04019846  0.34415171]]\n",
      "[[ 0.93525653  0.76660826]\n",
      " [ 0.01380756  0.55349255]\n",
      " [ 0.59566385  0.76267376]\n",
      " [ 0.91820398  0.32110088]\n",
      " [ 0.04019846  0.34415171]]\n"
     ]
    }
   ],
   "source": [
    "a = np.random.rand(10).reshape(5,2)\n",
    "print a\n",
    "np.save('a.npy',a)\n",
    "a_copy = np.load('a.npy')\n",
    "print a_copy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "保存多个变量时，等号前面是变量的key，从结果r里面可以取到。\n",
    "\n",
    "如果不使用key来保存a，那么通过“arr_0”,\"arr_1\"来取到。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.93525653  0.76660826]\n",
      " [ 0.01380756  0.55349255]\n",
      " [ 0.59566385  0.76267376]\n",
      " [ 0.91820398  0.32110088]\n",
      " [ 0.04019846  0.34415171]] \n",
      "\n",
      "[0 1 2 3 4]\n"
     ]
    }
   ],
   "source": [
    "b = range(5)\n",
    "np.savez('a_and_b.npz',a,bdata=b)\n",
    "r = np.load('a_and_b.npz')\n",
    "print r[\"arr_0\"],'\\n'*2,r[\"bdata\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "最后，对txt，csv，tsv的读写：\n",
    "\n",
    "csv意为comma separated values\n",
    "\n",
    "tsv意为tab separated values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.93525653  0.76660826]\n",
      " [ 0.01380756  0.55349255]\n",
      " [ 0.59566385  0.76267376]\n",
      " [ 0.91820398  0.32110088]\n",
      " [ 0.04019846  0.34415171]]\n"
     ]
    }
   ],
   "source": [
    "np.savetxt(\"a.txt\",a,delimiter = '\\t')\n",
    "a_copy2 = np.loadtxt(\"a.txt\",delimiter = '\\t')\n",
    "print a_copy2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
